package com.le.t1;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

public class zcjm {
    private Socket socket;

    private InetAddress serverIP= null;


    private ObjectOutputStream os=null;
    person person;
    public zcjm(){
         JFrame frame=new JFrame("远程注册");
        JLabel label1=new JLabel("账号");
        JLabel label2=new JLabel("密码");
        JLabel label3=new JLabel("确认密码");

        JTextField textField1=new JTextField(50);
        JTextField textField2=new JTextField(50);
        JTextField textField3=new JTextField(50);
        JButton button1=new JButton("注册");
        JButton button2=new JButton("返回");
        frame.setLayout(null);
        frame.setBounds(200,200,500,500);

        label1.setBounds(100,100,50,30);
        textField1.setBounds(150,100,100,30);
        label2.setBounds(100,130,50,30);
        textField2.setBounds(150,130,100,30);
        label3.setBounds(100,160,50,30);
        textField3.setBounds(150,160,100,30);
        button1.setBounds(120,200,60,30);
        button2.setBounds(180,200,60,30);
        frame.setLocationRelativeTo(null);
        frame.add(label1);
        frame.add(label2);
        frame.add(label3);
        frame.add(textField1);
        frame.add(textField2);
        frame.add(textField3);
        frame.add(button1);
        frame.add(button2);
        button1.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {


                try {

                    String t1=textField1.getText();

                    String t2=textField2.getText();

                    String t3=textField3.getText();

                    if (t1!="" && t2!="" && t3!="" && t2.equals(t3)) {


                        serverIP = InetAddress.getByName("127.0.0.1");
                        person = new person(textField1.getText(), textField2.getText());
                        //2.创建socket连接
                        socket = new Socket(serverIP, 9999);
                        //发送消息
                        os = new ObjectOutputStream(socket.getOutputStream());

                        os.writeObject(person);
                        JOptionPane.showMessageDialog(null,"注册成功");
                    }else {
                        JOptionPane.showMessageDialog(null,"注册失败");
                    }



                } catch (UnknownHostException ex) {
                    ex.printStackTrace();
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                } catch (Exception exception) {
                    exception.printStackTrace();
                } finally {
                    try {
                        if (os!=null) {
                            os.close();
                        }
                        if(socket!=null) {
                            socket.close();
                        }
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }

                }

            }
        });


        button2.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.setVisible(false);
                new client();
            }
        });


        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setVisible(true);

    }
}
